<?php
namespace app\patrol\controller;
use app\patrol\logic\DayPatolLogic;
use think\Db;

/**
 * 日常巡店api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/24
 * Time: 09:36
 */
class DayPatrol extends Patrol {

    private $cp_type = 2;

    public function __construct(){
        parent::__construct();
        $this->dp_logic = new DayPatolLogic();
    }

    /**
     * 获取日常巡店楼层
     * @return \think\response\Json
     */
    public function ajax_get_dp_floor(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->dp_logic->get_search_floor_maps($params['branchid']);
        $list = model('floor')->get_list($maps,'id,name');

        $data['list'] = $list;
        return json($this->dp_logic->get_response(1,'OK',$data));
    }

    /**
     * 开始日常巡店
     * @return \think\response\Json
     */
    public function ajax_start_dp(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->dp_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查开始巡店规则
        $r = $this->dp_logic->check_start_patrol_floor_rule($params['floorid']);
        if(!$r['code']){
            return json($r);
        }
        $f_detail = $r['data']['f_detail'];

        Db::startTrans();
        //生成巡店楼层
        $r = $this->dp_logic->gen_patrol_floor($u_detail['openid'],$this->cp_type,0,$params['branchid'],$f_detail['id']);
        if(!$r['code']){
            Db::rollback();
            return json($r);
        }
        $cp_floorid = $r['data']['cp_floorid'];

        //生成巡店区域和模块
        $r = $this->dp_logic->gen_patrol_region_modular($this->cp_type,0,$cp_floorid,$params['branchid'],$f_detail['id']);
        if(!$r['code']){
            Db::rollback();
            return json($r);
        }
        Db::commit();

        $data['cp_floorid'] = $cp_floorid;
        return json($this->dp_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取日常巡店
     * @return \think\response\Json
     */
    public function ajax_get_dp(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->dp_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //获取巡店区域和模块
        $r = $this->dp_logic->get_patrol_region_modular($u_detail['openid'],$this->cp_type,$params['cp_floorid']);
        if(!$r['code']){
            return json($r);
        }
        $list = $r['data']['list'];

        //获取社区巡店描述常用语
        $maps = [];
        $maps[] = ['key','=','cp_desc_use_words'];
        $conf_info = $this->dp_logic->get_conf_info($maps);
        $conf_info['cp_desc_use_words'] = json_decode($conf_info['cp_desc_use_words'],true);

        $data['list'] = $list;
        $data['conf_info'] = $conf_info;
        return json($this->dp_logic->get_response(1,'OK',$data));
    }

    /**
     * 更新日常巡店区域和模块
     * @return \think\response\Json
     */
    public function ajax_save_dp_region_modular(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->dp_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查更新巡店区域和模块规则
        $r = $this->dp_logic->check_save_patrol_region_modular_rule($this->cp_type,$params);
        if($r['code'] != 1){
            return json($r);
        }
        $crm_detail = $r['data']['crm_detail'];

        //更新巡店区域和模块
        $r = $this->dp_logic->save_patrol_region_modular($u_detail['openid'],$params,$crm_detail);
        if(!$r['code']){
            return json($r);
        }

        //更新日常巡店楼层
        $r = $this->dp_logic->save_dp_floor($u_detail['openid'],$this->cp_type,$crm_detail);
        if(!$r['code']){
            return json($r);
        }
        return json($this->dp_logic->get_response(1,'OK'));
    }

    /**
     * 获取日常巡店工位
     * @return \think\response\Json
     */
    public function ajax_get_dp_ws(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $now_date = date('Y-m-d');
        $dp = new \app\common\controller\Dp();

        //处理日常巡店工位“是否加班”显示
        $is_show_work_arr = $dp->deal_dp_is_show_work();

        $maps = $this->dp_logic->get_search_ws_maps($params['regionid']);
        $ws_list = model('work_station')->get_list($maps);
        $list = [];
        foreach($ws_list as $k => $v){
            $ws_list[$k]['work_type_zw'] = $v->work_type_name;
            $ws_list[$k]['price'] = deal_number_div_100($v['price']);
            $ws_list[$k]['is_indoor_air_zw'] = $v->is_indoor_air_name;
            $ws_list[$k]['ws_pic'] = !empty($v['ws_pic']) ? json_decode($v['ws_pic'],true) : [];
            $ws_list[$k]['company_pic'] = !empty($v['company_pic']) ? json_decode($v['company_pic'],true) : [];
            $ws_list[$k]['company_work_preson'] = !empty($v['company_work_preson']) ? json_decode($v['company_work_preson'],true) : [];

            //查找区域
            $r_maps = [];
            $r_maps[] = ['id','=',$v['regionid']];
            $r_detail = model('region')->get_detail($r_maps,'id,floorid,name');
            $ws_list[$k]['region_zw'] = $r_detail['name'];

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$r_detail['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
            $ws_list[$k]['floor_zw'] = $f_detail['name'];

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $ws_list[$k]['branch_zw'] = $b_detail['name'];

            //获取巡店工位信息
            $cp_ws_info = $dp->get_patrol_ws_info($v['regionid'],$v['id']);
            $ws_list[$k]['use_status'] = $cp_ws_info['use_status'];
            $ws_list[$k]['use_status_zw'] = $cp_ws_info['use_status_zw'];
            $ws_list[$k]['use_rate'] = $cp_ws_info['use_rate'];
            $ws_list[$k]['person_num'] = $cp_ws_info['person_num'];
            $ws_list[$k]['is_holiday_work'] = $cp_ws_info['is_holiday_work'];
            $ws_list[$k]['is_evening_work'] = $cp_ws_info['is_evening_work'];
            $ws_list[$k]['ccids'] = $cp_ws_info['ccids'];
            $ws_list[$k]['remark'] = $cp_ws_info['remark'];
            $ws_list[$k]['status'] = $cp_ws_info['status'];
            $ws_list[$k]['status_zw'] = $cp_ws_info['status_zw'];

            //计算剩余天数
            $ws_list[$k]['rest_day'] = get_time_diff_day($v['company_out_time'],$now_date);

            //计算空置天数
            $ws_list[$k]['vacant_day'] = get_time_diff_day($now_date,$v['company_out_time']);

            //是否显示“晚上是否加班”
            $ws_list[$k]['is_show_evening_work'] = $is_show_work_arr['is_show_evening_work'];
            //是否显示“周末/节假日是否加班”
            $ws_list[$k]['is_show_holiday_work'] = $is_show_work_arr['is_show_holiday_work'];

            $list[$v['id']] = $v->toArray();
        }

        //获取社区巡店工位客户交流
        $maps = [];
        $maps[] = ['key','=','cp_ws_cc'];
        $conf_info = $this->dp_logic->get_conf_info($maps);
        $conf_info['cp_ws_cc'] = json_decode($conf_info['cp_ws_cc'],true);

        $data['list'] = $list;
        $data['conf_info'] = $conf_info;
        return json($this->dp_logic->get_response(1,'OK',$data));
    }

    /**
     * 添加日常巡店工位
     * @return \think\response\Json
     */
    public function ajax_add_dp_ws(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->dp_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $dp = new \app\common\controller\Dp();

        $ws_list = $params['ws_data'];
        foreach($ws_list as $k => $v){
            if($v['status'] == 1){
                $maps = [];
                $maps[] = ['cp_type','=',$this->cp_type];
                $maps[] = ['cp_floorid','=',$v['cp_floorid']];
                $maps[] = ['branchid','=',$params['branchid']];
                $maps[] = ['floorid','=',$v['floorid']];
                $maps[] = ['regionid','=',$v['regionid']];
                $maps[] = ['wsid','=',$v['wsid']];
                $crw_detail = model('cp_region_ws')->get_detail($maps,'id');
                if(empty($crw_detail)){
                    $ws_data = [];
                    $ws_data['openid'] = $u_detail['openid'];
                    $ws_data['cp_type'] = $this->cp_type;
                    $ws_data['cp_branchid'] = 0;
                    $ws_data['cp_floorid'] = $v['cp_floorid'];
                    $ws_data['branchid'] = $params['branchid'];
                    $ws_data['floorid'] = $v['floorid'];
                    $ws_data['regionid'] = $v['regionid'];
                    $ws_data['wsid'] = $v['wsid'];
                    $ws_data['use_status'] = $v['use_status'];
                    $ws_data['use_rate'] = trim($v['use_rate']);
                    $ws_data['person_num'] = trim($v['person_num']);
                    $ws_data['is_holiday_work'] = $v['is_holiday_work'];
                    $ws_data['is_evening_work'] = $v['is_evening_work'];
                    $ws_data['ccids'] = json_encode($v['ccids'],JSON_UNESCAPED_UNICODE);
                    $ws_data['remark'] = trim($v['remark']);
                    $ws_data['status'] = 1;

                    $crwid = model('cp_region_ws')->insertGetId($ws_data);

                    //更新写备忘录积分
                    $si = new \app\common\controller\SaveIntegral();
                    $r = $si->save_wr_integral($crwid);
                }else{
                    $ws_data = [];
                    $ws_data['use_status'] = $v['use_status'];
                    $ws_data['use_rate'] = trim($v['use_rate']);
                    $ws_data['person_num'] = trim($v['person_num']);
                    $ws_data['is_holiday_work'] = $v['is_holiday_work'];
                    $ws_data['is_evening_work'] = $v['is_evening_work'];
                    $ws_data['ccids'] = json_encode($v['ccids'],JSON_UNESCAPED_UNICODE);
                    $ws_data['remark'] = trim($v['remark']);

                    $maps = [];
                    $maps[] = ['id','=',$crw_detail['id']];
                    $r = model('cp_region_ws')->force()->save($ws_data,$maps);

                    $crwid = $crw_detail['id'];
                }

                //计算巡店工位使用率
                $use_rate_arr = $dp->count_patrol_ws_use_rate(1,$v['wsid']);
                $ws_data = [];
                $ws_data['use_rate'] = $use_rate_arr['use_rate'];

                $maps = [];
                $maps[] = ['id','=',$crwid];
                $r = model('cp_region_ws')->force()->save($ws_data,$maps);
            }
        }
        return json($this->dp_logic->get_response(1,'添加成功'));
    }
}
